package com.hc.api.controller;

import com.hc.api.enums.OrderStateEnum;
import com.hc.api.utils.ApiUtil;
import com.hc.comm.base.BaseController;
import com.hc.comm.dto.UserdataDTO;
import com.hc.comm.dto.VoucherDataDTO;
import com.hc.comm.utils.JsonUtils;
import com.hc.comm.vo.OrderVO;
import com.hc.comm.vo.VoucherDataVO;
import com.hc.comm.vo.VoucherTypeVO;
import com.hc.service.model.OrderModel;
import com.hc.service.model.UserdataModel;
import com.hc.service.model.VoucherDataModel;
import com.hc.service.model.VoucherTypeModel;
import com.hc.service.service.IOrderService;
import com.hc.service.service.IUserdataService;
import com.hc.service.service.IVoucherDataService;
import com.hc.service.service.IVoucherTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 优惠券订单controller
 * Created by yx on 2017/5/8.
 */
@Controller
@RequestMapping("/" + ApiUtil.ApiVersion.V1 + "/buy/")
public class VoucherOrderController extends BaseController {

    @Resource(name = "userdataService")
    private IUserdataService userdataService;

    @Resource(name = "voucherDataService")
    private IVoucherDataService voucherDataService;

    @Resource(name = "orderService")
    private IOrderService orderService;

    @Resource(name = "voucherTypeService")
    private IVoucherTypeService voucherTypeService;

    /**
     * 购买优惠券，获取订单
     * voucherId  优惠券id
     * userId   用户id
     * buyNum   购买数量
     *
     * @throws Exception
     */
    @RequestMapping("buyVoucher")
    public void buyVoucher() throws Exception {
        int voucherId = Integer.valueOf(request.getParameter("voucherId"));
        int userId = Integer.valueOf(request.getParameter("userId"));
        int buyNum = Integer.valueOf(request.getParameter("buyNum"));

        if (voucherId == 0 || userId == 0
                || buyNum == 0) {
            JsonUtils.toErrorResult(request, response, "参数错误!");
            return;
        }
        UserdataDTO userdataDTO = new UserdataDTO();
        userdataDTO.setId(Long.valueOf(userId));
        UserdataModel userdataModel = userdataService.getOne(userdataDTO);
        if (userdataModel == null) {
            JsonUtils.toErrorResult(request, response, "没有该用户!");
            return;
        }
        VoucherDataDTO voucherDataDTO = new VoucherDataDTO();
        voucherDataDTO.setId(voucherId);

        VoucherDataModel voucherDataModel = voucherDataService.getOne(voucherDataDTO);
        if (voucherDataModel == null) {
            JsonUtils.toErrorResult(request, response, "没有该优惠券!");
            return;
        }
        if (voucherDataModel.getStockNum() == null
                || voucherDataModel.getStockNum() < 0
                || voucherDataModel.getStockNum() < buyNum) {
            JsonUtils.toErrorResult(request, response, "库存不足!");
            return;
        }
        OrderModel orderModel = new OrderModel();
        orderModel.setMakeTime(new Long(System.currentTimeMillis() / 1000).intValue());
        orderModel.setUserId(userId);
        orderModel.setVoucherId(voucherId);
        orderModel.setVoucherNum(buyNum);
        orderModel.setOrderState(OrderStateEnum.UNPAY.getCode());
        orderModel.setOrderMoney(voucherDataModel.getVoucherPrice());
        orderModel.setCreateTime(new Long(System.currentTimeMillis() / 1000).intValue());
        orderModel.setUpdateTime(new Long(System.currentTimeMillis() / 1000).intValue());
        orderModel = orderService.insert(orderModel);

        if (orderModel != null) {
            OrderVO orderVO = new OrderVO();
            orderVO.copyPropertis(orderModel);
            VoucherDataVO voucherDataVO = new VoucherDataVO();
            voucherDataVO.copyPropertis(voucherDataModel);
            VoucherTypeModel voucherTypeModel = voucherTypeService.getByPrimaryKey(voucherDataModel.getVoucherType());
            VoucherTypeVO voucherTypeVO = new VoucherTypeVO();
            voucherTypeVO.copyPropertis(voucherTypeModel);
            voucherDataVO.setVoucherTypeVO(voucherTypeVO);
            orderVO.setVoucherDataVO(voucherDataVO);
            JsonUtils.toSuccessResult(request, response, orderVO,
                    "id", "userId", "voucherId", "orderMoney", "voucherNum",
                    "orderState", "makeTime", "voucherDataVO",
                    "voucherName", "voucherType", "hospitalId", "voucherDescription", "voucherIcon",
                    "voucherPrice", "voucherState", "stockNum", "startTime", "endTime",
                    "voucherMoney", "voucherTypeVO", "typeName", "typeIcon");
            return;
        }
    }
}

